package database

import (
	"fmt"

	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

var (
	DB *gorm.DB
)

func init() {

	// DB.AutoMigrate(&User{}, &XXXX{})
	// 连接数据库
	// db, err := gorm.Open(sqlite.Open("example.db"), &gorm.Config{})
	// Mysql.Open("user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local")
}

func InitDatabase(host, port, dbname, user, passwd string) {
	//// postgres
	// dsn := fmt.Sprintf("host=server.lan dbname=iptvserver user=postgres password=123456 port=15433 sslmode=disable TimeZone=Asia/Shanghai")
	dsn := fmt.Sprintf("host=%s dbname=%s user=%s password=%s port=%s sslmode=disable TimeZone=Asia/Shanghai",
		host, dbname, user, passwd, port,
	)
	if db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
		DisableForeignKeyConstraintWhenMigrating: true,
	}); err != nil {
		fmt.Printf("err: %v\n", err)
		panic(err)
	} else {
		DB = db
	}
}
